package com.openatc.agent.service.impl;

import com.openatc.agent.service.FeatureService;
import com.openatc.agent.service.GenericMsgStrategy;
import com.openatc.comm.data.MessageData;
import com.openatc.core.model.RESTRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Classname FeatureOverlapRequest
 * @Description 跟随相位处理类
 * @Date 2022/7/20 9:05
 * @Created by panleilei
 */
@Service(value = "feature/overlap")
public class FeatureOverlapRequest extends GenericMsgStrategy {
    @Autowired
    private FeatureService featureService;
    @Autowired
    private CommServiceDown commServiceDown;
    /**
     * 成功下载跟随相位
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet successSetProcess(MessageData requestData, RESTRet responseData) {
        String agentid = requestData.getAgentid();
        RESTRet restRet = featureService.deleteFeatureByAgentid(agentid);
        if (!restRet.isSuccess()) {
            logger.warning("MsgPostHandler process: clear feature failed!");
        }
        // 更新dev表中overlap字段的值
        int row = updateDevOverlapCount(requestData);
        if (row == 0) {
            logger.warning("update dev overlap count failed!");
        }
        return super.successSetProcess(requestData, responseData);
    }

    private int updateDevOverlapCount(MessageData requestData) {
        return commServiceDown.updateDevFeatureCount("overlaplList",FieldName.OVERLAP_COUNT,requestData);
    }

    @Override
    public RESTRet successGetProcess(MessageData requestData, RESTRet responseData) {
        MessageData res = (MessageData) responseData.getData();
        // 更新dev表中overlap字段的值
        int row = updateDevOverlapCount(res);
        if (row == 0) {
            logger.warning("update dev overlap count failed!");
        }
        return super.successGetProcess(requestData, responseData);
    }
}
